package com.woniuxy.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.mapper.CustomersMapper;
import com.woniuxy.mapper.RechargeRecordMapper;
import com.woniuxy.model.entity.Customers;
import com.woniuxy.model.entity.RechargeRecord;
import com.woniuxy.service.RechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
* @author Administrator
* @description 针对表【recharge_record】的数据库操作Service实现
* @createDate 2025-01-08 15:26:24
*/
@Service
public class RechargeRecordServiceImpl extends ServiceImpl<RechargeRecordMapper, RechargeRecord>
    implements RechargeRecordService {


    @Autowired
    private CustomersMapper customersMapper;

    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;

    @Transactional
    @Override
    public void recharge(Long customerId, BigDecimal rechargeAmount, Long paymentId) {
        // 更新客户的 points
        Customers customer = customersMapper.selectById(customerId);
        if (customer == null) {
            throw new RuntimeException("客户不存在");
        }

        customer.setPoints(customer.getPoints() + rechargeAmount.intValue());
        customersMapper.updateById(customer);

        // 生成充值记录
        RechargeRecord rechargeRecord = new RechargeRecord();
        rechargeRecord.setOrderNo(UUID.randomUUID().toString().replace("-", ""));
        rechargeRecord.setCreateTime(new Date());
        rechargeRecord.setCustomerId(customerId);
        rechargeRecord.setPaymentId(paymentId);
        rechargeRecord.setRechargeAmount(rechargeAmount);
        rechargeRecord.setAccountBalance(new BigDecimal(customer.getPoints())); // 当前积分作为账户余额
        rechargeRecord.setTransactionStatus("成功");
        rechargeRecord.setRemark("充值成功");

        rechargeRecordMapper.insert(rechargeRecord);
    }

}




